﻿Public Class frmLoaiHang
    Private Sub frmLoaiHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Quản lí loại hàng"
        dgvLoaiHang.DataSource = LoaiHangDAO.LoadLoaiHang()
    End Sub
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim frm = New frmThem_CapNhatLoaiHang
        frm._status = "Them"
        AddHandler frm.FormClosed, AddressOf frmLoaiHang_FormClosed
        frm.Show()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim frm = New frmThem_CapNhatLoaiHang
        frm._status = "CapNhat"
        frm._id = Convert.ToInt32(dgvLoaiHang.Rows(dgvLoaiHang.CurrentCell.RowIndex).Cells("ID").Value.ToString())
        frm.txtLoai.Text = dgvLoaiHang.Rows(dgvLoaiHang.CurrentCell.RowIndex).Cells("Loai").Value.ToString()
        AddHandler frm.FormClosed, AddressOf frmLoaiHang_FormClosed
        frm.Show()
    End Sub

    Private Sub frmLoaiHang_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        dgvLoaiHang.DataSource = LoaiHangDAO.LoadLoaiHang()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim dr = New DialogResult
        dr = MessageBox.Show("Bạn có muốn xóa", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
        If DialogResult.Yes = dr Then
            Dim _lh = New LoaiHang
            _lh.m_intID = Convert.ToInt32(dgvLoaiHang.Rows(dgvLoaiHang.CurrentCell.RowIndex).Cells("ID").Value.ToString())
            LoaiHangDAO.XoaLoaiHang(_lh)
            dgvLoaiHang.DataSource = LoaiHangDAO.LoadLoaiHang()
        Else
            Return
        End If
        
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        me.Close()
    End Sub

    Private Sub txtTimKiem_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTimKiem.TextChanged
        If txtTimKiem.Text = "" Then
            dgvLoaiHang.DataSource = LoaiHangDAO.LoadLoaiHang()
        Else
            Dim _lh = New LoaiHang
            _lh.m_strLoai = txtTimKiem.Text
            dgvLoaiHang.DataSource = LoaiHangDAO.TimKiemLoaiHang(_lh)
        End If
    End Sub
End Class